/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author carlos
 */
import java.io.*;
import java.util.*;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int option = -1;
        boolean exit = false;
        String input = "";
        String[] lines;
        String ret;
        ValuateList list = new ValuateList();
        
        // Pedir opción del menú
        do{
            printMenu(0);
            option = getOption();
        }while(option == -1);
        
        // Opciones para el programa
        switch(option){
            case 1:
                do{
                    lines = getFile();
                    for(int i = 0; i != lines.length; i++){
                        ret = list.getResult(lines[i]);
                        if(ret.compareTo("exit") == 0){
                            exit = true;
                            break;
                        }
                        if(ret == null){
                            ret = "Error...!!!";
                        }
                        printMenu(1);
                        System.out.println(lines[i]);
                        System.out.println("Resultado: " + ret + "\n");
                    }
                }while(!exit);
                break;
            case 2:
                do{
                   printMenu(1);
                   input = getString();
                   if(input.compareTo("exit") == 0){
                       exit = true;
                       break;
                   }
                   ret = list.getResult(input);
                   if(ret == null){
                       ret = "Error...!!!";
                   }
                   System.out.println("\nResultado: " + ret);
                }while(!exit);
                break;
            case 3:
                break;
        }
        
        printMenu(2);
        
    }
    
    
    /**
     * 
     * @param op El menú a imprimir
     */
    private static void printMenu(int op){
        switch(op){
            case 0:
                System.out.println("Bienvenido a CFLISP...");
                System.out.println("1. Seleccionar desde archivo");
                System.out.println("2. Interprete");
                System.out.println("3. Salir del programa");
                break;
            case 1:
                System.out.print("@CFLISP: ");
                break;
            case 2:
                System.out.println("Gracias por utilizar el programa...!");
                break;
            case 3:
                System.out.println("El archivo no se ha encontrado...!!!");
                break;
        }
    }
    
    private static int getOption(){
        int ret = -1;
        Scanner scan = new Scanner(System.in);
        
        try{
            ret = scan.nextInt();
        }catch(Exception e){
            System.out.println("Ingrese un numero correctamente...");
        }
        return ret;
    }
    
    /**
     * 
     * @return El texto ingresado por teclado
     */
    private static String getString(){
        String ret = "";
        Scanner scan = new Scanner(System.in);
        
        try{
            ret = scan.nextLine();
        }catch(Exception e){
            
        }
        
        return ret;
    }
    
    /**
     * 
     * @return La cadena con el archivo mostrado
     */
    private static String[] getFile(){
        String[] ret = null;
        String lines = "";
        String fileName = "";
        File file = null;
        Scanner scan = new Scanner(System.in);
        Scanner fileReader = null;
        
        try{
            fileName = scan.nextLine();
        }catch(Exception e){
            
        }
        
        try{
            file = new File(fileName);
        }catch(Exception e){
            printMenu(3);
        }
        
        try{
            fileReader = new Scanner(file);
        }catch(Exception e){
            printMenu(3);
        }
        
        while(fileReader.hasNextLine()){
            lines += fileReader.nextLine();
        }
        
        ret = lines.split("\n");
        
        return ret;
    }
}
